package com.fuqi.android.zhongcai.business.shuig.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.result.bean.Question;
import com.fuqi.android.zhongcai.base.request.result.bean.VisitBean;

import java.util.ArrayList;
import java.util.List;

public class AnswerQuestion2Adapter extends BaseAdapter {
	private List<VisitBean> listBean = new ArrayList<VisitBean>();
	private Context context;
	private List<Question> questions;

	public AnswerQuestion2Adapter(Context context, List<VisitBean> listBean) {
		this.context = context;
		this.listBean = listBean;
		questions= new ArrayList<>();
	}
	public void setDate(List<VisitBean> listBean) {
		this.listBean = listBean;
		notifyDataSetChanged();
	}

	public void setQuestions(List<Question> questions){
		this.questions = questions;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listBean.size();
	}

	@Override
	public VisitBean getItem(int position) {
		return listBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_huifang, null);
			viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.gvItem = (GridView) convertView.findViewById(R.id.gv_item);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		VisitBean question = listBean.get(position);

		List<VisitBean> datass = question.getRls();

		AnswerAdapter mTypeAdapter = new AnswerAdapter(context,datass);
		viewHolder.tvTitle.setText(question.getName());
		viewHolder.gvItem.setNumColumns(getNumColumns(datass));
		viewHolder.gvItem.setAdapter(mTypeAdapter);
		if(questions!=null)
		for (int i = 0;i < questions.size();i++){
			if(questions.get(i).getQuestionCode().equals(question.getBaseCode())) {
				for (int j = 0;j < datass.size();j++)
					if(datass.get(j).getBaseCode().equals(questions.get(i).getResult())) {
						viewHolder.gvItem.setItemChecked(j, true);
					}
//					else
//						viewHolder.gvItem.setItemChecked(j, false);
			}
		}
		viewHolder.gvItem.setEnabled(false);
		return convertView;
	}

	private class ViewHolder{
        TextView tvTitle;
        GridView gvItem;
    }

	private int getNumColumns(List<VisitBean> datass){
		for (VisitBean bean : datass) {
			if(bean.getName().length()>8)
				return 1;
		}
		return 2;
	}
}
